<template>
  <div>
     <Row>
       <label>渠道号:</label>
        <Select v-model="bussiness" style="width:200px" not-found-text="-1">
          <Option v-for="item in tableList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
        <Date-picker v-model="startDay" placeholder="开始日期" format="yyyy-MM-dd"></Date-picker>
        <Date-picker v-model="endDay" placeholder="结束日期" format="yyyy-MM-dd"></Date-picker>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" @click="onRefresh">刷新统计</Button>
    </Row>
    </br>
    <Table :border="true" :stripe="true" :show-header="true" :data="tableTotalData" :columns="tableTotalColumns"></Table>
    </br>
    <Table :border="true" :stripe="true" :show-header="true" :height="500" :data="tableData" :columns="tableColumns"></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
  </div>
 
</template>
<script>

import {getRoleKeepAnalys, refreshRoleKeepAnalys, getRoleKeepAnalysBussiness} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
export default {
  data () {
    return {
      tableTotalData: [],
      tableData: [],
      startDay: '',
      endDay: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      bussiness: '-1',
      tableList: []
    }
  },
  created () {
    this.endDay = new Date()
    this.startDay = new Date().setDate(new Date().getDate() - 7)
    // 获取留存渠道号
    getRoleKeepAnalysBussiness().then((data) => {
      if (!data) {
        return
      }
      this.tableList = []
      let pv = data.data.data
      let len = pv.strValues.length
      for (let i = 0; i < len; i++) {
        let vo = {}
        vo.value = pv.strValues[i]
        if (vo.value === '-1') {
          vo.label = '全渠道'
        } else {
          vo.label = pv.strValues[i]
        }
        this.tableList.push(vo)
      }
      console.log(this.tableList)
      this.onSearch()
    })
  },
  methods: {
    onSearch () {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }
      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }

      console.log('bussiness:', this.bussiness, ',sSendDay:', sSendDay, ',eSendDay:', eSendDay, ',crtPage:', this.crtPage, ',pageSize:', this.pageSize)
      getRoleKeepAnalys(sSendDay, eSendDay, this.bussiness, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let RoleKeepAnalysVO = getVO('RoleKeepAnalysVO')
        let pv = data.data.data
        let len = pv.data.length
        let rate = 0
        for (let i = 0; i < len; i++) {
          let vo = RoleKeepAnalysVO.decode(pv.data[i])
          let _item = {}
          _item.id = vo.id
          _item.day = vo.day
          _item.gameId = vo.gameId
          _item.loginCount = vo.loginCount
          _item.addCount = vo.addCount
          rate = (vo.keepRate1 / vo.addCount) * 100
          rate = rate.toFixed(2)
          _item.keepRate1 = vo.keepRate1 + '(' + rate + '%)'
          rate = (vo.keepRate3 / vo.addCount) * 100
          rate = rate.toFixed(2)
          _item.keepRate3 = vo.keepRate3 + '(' + rate + '%)'
          rate = (vo.keepRate7 / vo.addCount) * 100
          rate = rate.toFixed(2)
          _item.keepRate7 = vo.keepRate7 + '(' + rate + '%)'
          rate = (vo.keepRate14 / vo.addCount) * 100
          rate = rate.toFixed(2)
          _item.keepRate14 = vo.keepRate14 + '(' + rate + '%)'
          rate = (vo.keepRate30 / vo.addCount) * 100
          rate = rate.toFixed(2)
          _item.keepRate30 = vo.keepRate30 + '(' + rate + '%)'
          _item.createTime = vo.createTime
          _item.lostCount = vo.lostCount
          _item.gameOperator = vo.gameOperator
          _item.newPlayerType = vo.newPlayerType
          this.tableData.push(_item)
        }
        this.totalPage = pv.intValues[1]
        // 总计
        this.tableTotalData = []
        let longlen = pv.longValues.length
        if (longlen >= 11) {
          let _item = {}
          let _loginTotal = pv.longValues[0]
          let _addTotal = pv.longValues[1]
          let _keepRate1Total = pv.longValues[2]
          let _keepRate3Total = pv.longValues[3]
          let _keepRate7Total = pv.longValues[4]
          let _keepRate14Total = pv.longValues[5]
          let _keepRate30Total = pv.longValues[6]
          let _addKeepRate3Total = pv.longValues[7]
          let _addKeepRate7Total = pv.longValues[8]
          let _addKeepRate14Total = pv.longValues[9]
          let _addKeepRate30Total = pv.longValues[10]
          _item.loginTotal = _loginTotal.toNumber()
          _item.addTotal = _addTotal.toNumber()
          rate = (_keepRate1Total / _addTotal) * 100
          rate = rate.toFixed(2)
          _item.keepRate1Total = _keepRate1Total + '(' + rate + '%)'
          rate = 0
          if (_addKeepRate3Total > 0) {
            rate = (_keepRate3Total / _addKeepRate3Total) * 100
            rate = rate.toFixed(2)
          }
          _item.keepRate3Total = _keepRate3Total + '(' + rate + '%)'
          rate = 0
          if (_addKeepRate7Total > 0) {
            rate = (_keepRate7Total / _addKeepRate7Total) * 100
            rate = rate.toFixed(2)
          }
          _item.keepRate7Total = _keepRate7Total + '(' + rate + '%)'
          rate = 0
          if (_addKeepRate14Total > 0) {
            rate = (_keepRate14Total / _addKeepRate14Total) * 100
            rate = rate.toFixed(2)
          }
          _item.keepRate14Total = _keepRate14Total + '(' + rate + '%)'
          rate = 0
          if (_addKeepRate30Total > 0) {
            rate = (_keepRate30Total / _addKeepRate30Total) * 100
            rate = rate.toFixed(2)
          }
          _item.keepRate30Total = _keepRate30Total + '(' + rate + '%)'
          this.tableTotalData.push(_item)
          console.log(this.tableTotalData)
        }
      })
    },
    onRefresh () {
      refreshRoleKeepAnalys().then((data) => {
        if (!data) {
          return
        }
        this.$Message.info('刷新成功!!!')
        console.log('refresh role keep analys end')
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: '时间',
          key: 'day',
          align: 'center'
        },
        {
          title: '登录人数',
          key: 'loginCount',
          align: 'center'
        },
        {
          title: '新增人数',
          key: 'addCount',
          align: 'center'
        },
        {
          title: '次留存',
          key: 'keepRate1',
          align: 'center'
        },
        {
          title: '3留存',
          key: 'keepRate3',
          align: 'center'
        },
        {
          title: '7留存',
          key: 'keepRate7',
          align: 'center'
        },
        {
          title: '14留存',
          key: 'keepRate14',
          align: 'center'
        },
        {
          title: '30留存',
          key: 'keepRate30',
          align: 'center'
        }
      ]
      return cols
    },
    tableTotalColumns () {
      let cols = [
        {
          title: '总计',
          key: 'total',
          align: 'center'
        },
        {
          title: '登录人数',
          key: 'loginTotal',
          align: 'center'
        },
        {
          title: '新增人数',
          key: 'addTotal',
          align: 'center'
        },
        {
          title: '次留存',
          key: 'keepRate1Total',
          align: 'center'
        },
        {
          title: '3留存',
          key: 'keepRate3Total',
          align: 'center'
        },
        {
          title: '7留存',
          key: 'keepRate7Total',
          align: 'center'
        },
        {
          title: '14留存',
          key: 'keepRate14Total',
          align: 'center'
        },
        {
          title: '30留存',
          key: 'keepRate30Total',
          align: 'center'
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}
</style>
